package com.ico.api.config.async;

import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 配置线程池
 *
 * @author ONEC
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {


//    @Value(value = "${spring.application.name}")
//    private String value;

    /**
     * 配置异步线程池数据
     * 使用异步线程池的时候：@Async("asyncExecutor")
     *
     * @return
     */
    @Bean
    public Executor asyncExecutor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(200);
        // 最大线程数
        executor.setMaxPoolSize(500);
        // 最大队列数
        executor.setQueueCapacity(600);
        // 线程名称开头:value +
        String asyncStr = DateUtil.formatDate(DateUtil.date());
        executor.setThreadNamePrefix(asyncStr);
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
